<?php
/**
 * 用户岗位关系表
 */
apf_require_class("Bll_BLL");
class Bll_Attendance_UserPositionInfo extends BLL{


    public function find_tree_by_position($position_id) {
        $result = array();
        if(empty($position_id)) {
            return $tree;
        }
        $rs = $this->get_default_dao()->find(null,"user_id asc",0);

        if(empty($rs)) {return $result;}

        $arr = array();
        foreach($rs as $v) {
            $arr[$v['position_id']]['user_name'][] = $v['user_name'];
            $arr[$v['position_id']]['position_name'] = $v['position_name'];
            $arr[$v['position_id']]['parent_position_id'] = $v['parent_position_id'];
            $arr[$v['position_id']]['status'] = $v['status'];
        }
        $result[$position_id] = $arr[$position_id];
        $child = $this->tree($arr,$position_id);
        if(!empty($child)) {
            $result[$position_id]['children'] = $child;
        }else {
            $result[$position_id]['leaf'] = 1;
        }
        return $result;
    }

    /*
     *
     <li><span>Item 1</span>
        <ul>
            <li><span>Item 1.0</span>
                <ul>
                    <li><span>Item 1.0.0</span></li>
                </ul>
            </li>
            <li><span>Item 1.1</span></li>
            <li><span>Item 1.2</span>
                <ul>
                    <li><span>Item 1.2.0</span>
                    <ul>
                        <li><span>Item 1.2.0.0</span></li>
                        <li><span>Item 1.2.0.1</span></li>
                        <li><span>Item 1.2.0.2</span></li>
                    </ul>
                </li>
                    <li><span>Item 1.2.1</span>
                    <ul>
                        <li><span>Item 1.2.1.0</span></li>
                    </ul>
                </li>
                    <li><span>Item 1.2.2</span>
                    <ul>
                        <li><span>Item 1.2.2.0</span></li>
                        <li><span>Item 1.2.2.1</span></li>
                        <li><span>Item 1.2.2.2</span></li>
                    </ul>
                </li>
                </ul>
            </li>
        </ul>
    </li>
     *
     */

    public function tree($table,$p_id) {
        $tree = array();
        foreach($table as $id => $row){
            if($row['parent_position_id']==$p_id){
                $tmp = $this->tree($table,$id);
                if($tmp){
                    $row['children']=$tmp;
                }else{
                    $row['leaf'] = true;
                }
                unset($table[$id]);
                $tree[]=$row;
            }
        }
        return $tree;
    }



    private function get_default_dao() {
        return $this->get_dao('Dao_Attendance_UserPositionInfo');
    }
}
